<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Hoodoo::Presenters::Field</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/github.css" type="text/css" media="screen" />
<script src="../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>
    <div class="banner">
        <h1>
            <span class="type">Class</span>
            Hoodoo::Presenters::Field
                <span class="parent">&lt;
                    Object
                </span>
        </h1>
        <ul class="files">
            <li><a href="../../../files/lib/hoodoo/presenters/types/field_rb.html">lib/hoodoo/presenters/types/field.rb</a></li>
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
    <div class="description">
      
<p>A JSON schema member</p>

    </div>








    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
        <dt>F</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-i-full_path">full_path</a>
              </li>
          </ul>
        </dd>
        <dt>H</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-i-has_default-3F">has_default?</a>
              </li>
          </ul>
        </dd>
        <dt>N</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-c-new">new</a>
              </li>
          </ul>
        </dd>
        <dt>R</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-i-read_at_path">read_at_path</a>,
              </li>
              <li>
                <a href="#method-i-rename">rename</a>,
              </li>
              <li>
                <a href="#method-i-render">render</a>,
              </li>
              <li>
                <a href="#method-i-rewrite_path">rewrite_path</a>
              </li>
          </ul>
        </dd>
        <dt>V</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-i-validate">validate</a>
              </li>
          </ul>
        </dd>
        <dt>W</dt>
        <dd>
          <ul>
              <li>
                <a href="#method-i-walk">walk</a>
              </li>
          </ul>
        </dd>
    </dl>








      <!-- Section attributes -->
      <div class="sectiontitle">Attributes</div>
      <table border='0' cellpadding='5'>
          <tr valign='top' id='attribute-i-default'>
            <td class='attr-rw'>
              [RW]
            </td>
            <td class='attr-name'>default</td>
            <td class='attr-desc'><p>Default value, if supplied.</p></td>
          </tr>
          <tr valign='top' id='attribute-i-name'>
            <td class='attr-rw'>
              [RW]
            </td>
            <td class='attr-name'>name</td>
            <td class='attr-desc'><p>The name of the field.</p></td>
          </tr>
          <tr valign='top' id='attribute-i-required'>
            <td class='attr-rw'>
              [RW]
            </td>
            <td class='attr-name'>required</td>
            <td class='attr-desc'><p><code>true</code> if the field is required.</p></td>
          </tr>
      </table>


    <!-- Methods -->
      <div class="sectiontitle">Class Public methods</div>
        <div class="method">
          <div class="title method-title" id="method-c-new">
              <b>new</b>(name, options = {})
            <a href="../../../classes/Hoodoo/Presenters/Field.html#method-c-new" name="method-c-new" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Initialize a <a href="Field.html">Field</a> instance with the appropriate
name and options.</p>
<dl class="rdoc-list note-list"><dt>name
<dd>
<p>The JSON key.</p>
</dd><dt>options
<dd>
<p>A <code>Hash</code> of options, e.g. :required =&gt; true.</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-c-new_source')" id="l_method-c-new_source">show</a>
              </p>
              <div id="method-c-new_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/presenters/types/field.rb, line 23</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">initialize</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">options</span> = {})
  <span class="ruby-ivar">@name</span>     = <span class="ruby-identifier">name</span>.<span class="ruby-identifier">to_s</span>
  <span class="ruby-ivar">@required</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">has_key?</span>( <span class="ruby-value">:required</span> ) <span class="ruby-operator">?</span> <span class="ruby-identifier">options</span>[ <span class="ruby-value">:required</span> ] <span class="ruby-operator">:</span> <span class="ruby-keyword">false</span>
  <span class="ruby-ivar">@path</span>     = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">has_key?</span>( <span class="ruby-value">:path</span>     ) <span class="ruby-operator">?</span> <span class="ruby-identifier">options</span>[ <span class="ruby-value">:path</span>     ] <span class="ruby-operator">:</span> []

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">has_key?</span>( <span class="ruby-value">:default</span> )
    <span class="ruby-ivar">@has_default</span> = <span class="ruby-keyword">true</span>
    <span class="ruby-ivar">@default</span>     = <span class="ruby-constant">Hoodoo</span><span class="ruby-operator">::</span><span class="ruby-constant">Utilities</span>.<span class="ruby-identifier">stringify</span>( <span class="ruby-identifier">options</span>[ <span class="ruby-value">:default</span> ] )
  <span class="ruby-keyword">else</span>
    <span class="ruby-ivar">@has_default</span> = <span class="ruby-keyword">false</span>
    <span class="ruby-ivar">@default</span>     = <span class="ruby-keyword">nil</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
      <div class="sectiontitle">Instance Public methods</div>
        <div class="method">
          <div class="title method-title" id="method-i-full_path">
              <b>full_path</b>( path )
            <a href="../../../classes/Hoodoo/Presenters/Field.html#method-i-full_path" name="method-i-full_path" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Return the full path and name of this field</p>
<dl class="rdoc-list note-list"><dt>path
<dd>
<p>The JSON path or nil, e.g. &#39;one.two&#39;</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-full_path_source')" id="l_method-i-full_path_source">show</a>
              </p>
              <div id="method-i-full_path_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/presenters/types/field.rb, line 102</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">full_path</span>( <span class="ruby-identifier">path</span> )
  <span class="ruby-keyword">return</span> <span class="ruby-ivar">@name</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">path</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-keyword">or</span> <span class="ruby-identifier">path</span>.<span class="ruby-identifier">empty?</span>
  <span class="ruby-keyword">return</span> <span class="ruby-identifier">path</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-keyword">if</span> <span class="ruby-ivar">@name</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-keyword">or</span> <span class="ruby-ivar">@name</span>.<span class="ruby-identifier">empty?</span>
  <span class="ruby-identifier">path</span> <span class="ruby-operator">+</span> <span class="ruby-string">&#39;.&#39;</span> <span class="ruby-operator">+</span> <span class="ruby-ivar">@name</span>.<span class="ruby-identifier">to_s</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-i-has_default-3F">
              <b>has_default?</b>()
            <a href="../../../classes/Hoodoo/Presenters/Field.html#method-i-has_default-3F" name="method-i-has_default-3F" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Does this property have a defined default (which may be defined as
<code>nil</code>) rather than having no defined value (<code>nil</code> or
otherwise)? Returns <code>true</code> if it has a default,
<code>false</code> if it has no default.</p>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-has_default-3F_source')" id="l_method-i-has_default-3F_source">show</a>
              </p>
              <div id="method-i-has_default-3F_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/presenters/types/field.rb, line 41</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">has_default?</span>
  <span class="ruby-operator">!</span><span class="ruby-operator">!</span> <span class="ruby-ivar">@has_default</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-i-render">
              <b>render</b>( data, target )
            <a href="../../../classes/Hoodoo/Presenters/Field.html#method-i-render" name="method-i-render" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Dive down into a given hash along path array +@path+, building new hash
entries if necessary at each path level until the last one. At that last
level, assign the given object.</p>
<dl class="rdoc-list note-list"><dt><code>data</code>
<dd>
<p>The object to build at the final path entry - usually an empty <a
href="Array.html">Array</a> or <a href="Hash.html">Hash</a>.</p>
</dd><dt><code>target</code>
<dd>
<p>The <a href="Hash.html">Hash</a> (may be initially empty) in which to build
the path of keys from internal data +@path+.</p>
</dd></dl>

<p>Returns the full path array that was used (a clone of +@path+).</p>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-render_source')" id="l_method-i-render_source">show</a>
              </p>
              <div id="method-i-render_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/presenters/types/field.rb, line 73</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">render</span>( <span class="ruby-identifier">data</span>, <span class="ruby-identifier">target</span> )
  <span class="ruby-keyword">return</span> <span class="ruby-keyword">if</span> <span class="ruby-ivar">@name</span>.<span class="ruby-identifier">empty?</span>

  <span class="ruby-identifier">root</span>  = <span class="ruby-identifier">target</span>
  <span class="ruby-identifier">path</span>  = <span class="ruby-ivar">@path</span>.<span class="ruby-identifier">clone</span>
  <span class="ruby-identifier">final</span> = <span class="ruby-identifier">path</span>.<span class="ruby-identifier">pop</span>.<span class="ruby-identifier">to_s</span>

  <span class="ruby-identifier">path</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span> <span class="ruby-identifier">element</span> <span class="ruby-operator">|</span>
    <span class="ruby-identifier">element</span> = <span class="ruby-identifier">element</span>.<span class="ruby-identifier">to_s</span>
    <span class="ruby-identifier">root</span>[ <span class="ruby-identifier">element</span> ] = {} <span class="ruby-keyword">unless</span> <span class="ruby-identifier">root</span>.<span class="ruby-identifier">has_key?</span>( <span class="ruby-identifier">element</span> )
    <span class="ruby-identifier">root</span> = <span class="ruby-identifier">root</span>[ <span class="ruby-identifier">element</span> ]
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">root</span>[ <span class="ruby-identifier">final</span> ] = <span class="ruby-identifier">data</span>
  <span class="ruby-keyword">return</span> <span class="ruby-identifier">path</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">final</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-i-validate">
              <b>validate</b>( data, path = &#39;&#39; )
            <a href="../../../classes/Hoodoo/Presenters/Field.html#method-i-validate" name="method-i-validate" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Check if data is required and return a <a
href="../Errors.html">Hoodoo::Errors</a> instance.</p>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-validate_source')" id="l_method-i-validate_source">show</a>
              </p>
              <div id="method-i-validate_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/presenters/types/field.rb, line 47</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">validate</span>( <span class="ruby-identifier">data</span>, <span class="ruby-identifier">path</span> = <span class="ruby-string">&#39;&#39;</span> )
  <span class="ruby-identifier">errors</span> = <span class="ruby-constant">Hoodoo</span><span class="ruby-operator">::</span><span class="ruby-constant">Errors</span>.<span class="ruby-identifier">new</span>

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">data</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-ivar">@required</span>
    <span class="ruby-identifier">errors</span>.<span class="ruby-identifier">add_error</span>(
      <span class="ruby-string">&#39;generic.required_field_missing&#39;</span>,
      <span class="ruby-value">:message</span>   =<span class="ruby-operator">&gt;</span> <span class="ruby-node">&quot;Field `#{ full_path( path ) }` is required&quot;</span>,
      <span class="ruby-value">:reference</span> =<span class="ruby-operator">&gt;</span> { <span class="ruby-value">:field_name</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">full_path</span>( <span class="ruby-identifier">path</span> ) }
    )
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">errors</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-i-walk">
              <b>walk</b>( &amp;block )
            <a href="../../../classes/Hoodoo/Presenters/Field.html#method-i-walk" name="method-i-walk" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Invoke a given block, passing this item. See Hoodoo::Presenters::Base#walk
for why.</p>
<dl class="rdoc-list note-list"><dt>&amp;block
<dd>
<p>Mandatory block, which is passed &#39;self&#39; when called.</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-walk_source')" id="l_method-i-walk_source">show</a>
              </p>
              <div id="method-i-walk_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/presenters/types/field.rb, line 95</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">walk</span>( <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span> )
  <span class="ruby-identifier">block</span>.<span class="ruby-identifier">call</span>( <span class="ruby-keyword">self</span> )
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
      <div class="sectiontitle">Instance Protected methods</div>
        <div class="method">
          <div class="title method-title" id="method-i-read_at_path">
              <b>read_at_path</b>( from_target, with_path )
            <a href="../../../classes/Hoodoo/Presenters/Field.html#method-i-read_at_path" name="method-i-read_at_path" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Dive down into a given target data hash using the given array of path keys,
returning the result at the final key in the path. E.g. if the <a
href="Hash.html">Hash</a> is “{ :foo =&gt; { :bar =&gt; { :baz =&gt;
”hello“ } } }” then a path of “[ :foo, :bar ]” would yield “{ :baz =&gt;
”hello“ }”.</p>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-read_at_path_source')" id="l_method-i-read_at_path_source">show</a>
              </p>
              <div id="method-i-read_at_path_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/presenters/types/field.rb, line 115</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">read_at_path</span>( <span class="ruby-identifier">from_target</span>, <span class="ruby-identifier">with_path</span> )
  <span class="ruby-identifier">with_path</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span> <span class="ruby-identifier">element</span> <span class="ruby-operator">|</span>
    <span class="ruby-identifier">element</span> = <span class="ruby-identifier">element</span>.<span class="ruby-identifier">to_s</span>
    <span class="ruby-identifier">from_target</span> = <span class="ruby-identifier">from_target</span>[ <span class="ruby-identifier">element</span> ]
    <span class="ruby-keyword">break</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">from_target</span>.<span class="ruby-identifier">nil?</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">return</span> <span class="ruby-identifier">from_target</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-i-rename">
              <b>rename</b>( name )
            <a href="../../../classes/Hoodoo/Presenters/Field.html#method-i-rename" name="method-i-rename" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Rename a property to the given name. The internal name is changed and the
last path entry set to the same name (if a path is present). Paths of
sub-properties (if any) are updated with the parent&#39;s new name.</p>

<p>This is a specialist interface which is intended for internal use under
unusual circumstances.</p>
<dl class="rdoc-list note-list"><dt><code>name</code>
<dd>
<p>New property name. Must be a <a href="String.html">String</a>.</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-rename_source')" id="l_method-i-rename_source">show</a>
              </p>
              <div id="method-i-rename_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/presenters/types/field.rb, line 134</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">rename</span>( <span class="ruby-identifier">name</span> )
  <span class="ruby-identifier">depth</span> = <span class="ruby-ivar">@path</span>.<span class="ruby-identifier">count</span> <span class="ruby-operator">-</span> <span class="ruby-number">1</span>
  <span class="ruby-ivar">@name</span> = <span class="ruby-identifier">name</span>

  <span class="ruby-identifier">rewrite_path</span>( <span class="ruby-identifier">depth</span>, <span class="ruby-identifier">name</span> )
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
        <div class="method">
          <div class="title method-title" id="method-i-rewrite_path">
              <b>rewrite_path</b>( depth, name )
            <a href="../../../classes/Hoodoo/Presenters/Field.html#method-i-rewrite_path" name="method-i-rewrite_path" class="permalink">Link</a>
          </div>

            <div class="description">
              <p>Change the +@path+ array by writing a given value in at a given index. If
this property has any sub-properties, then those are recursively updated to
change the same depth item to the new name in all of them.</p>
<dl class="rdoc-list note-list"><dt><code>depth</code>
<dd>
<p>Index into +@path+ to make modifications.</p>
</dd><dt><code>name</code>
<dd>
<p>Value to write at that index.</p>
</dd></dl>
            </div>



            <div class="sourcecode">
              <p class="source-link">
                Source:
                <a href="javascript:toggleSource('method-i-rewrite_path_source')" id="l_method-i-rewrite_path_source">show</a>
              </p>
              <div id="method-i-rewrite_path_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/hoodoo/presenters/types/field.rb, line 148</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">rewrite_path</span>( <span class="ruby-identifier">depth</span>, <span class="ruby-identifier">name</span> )
  <span class="ruby-ivar">@path</span>[ <span class="ruby-identifier">depth</span> ] = <span class="ruby-identifier">name</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">depth</span> <span class="ruby-operator">&gt;=</span> <span class="ruby-number">0</span>
  <span class="ruby-keyword">return</span> <span class="ruby-keyword">if</span> <span class="ruby-ivar">@properties</span>.<span class="ruby-identifier">nil?</span>

  <span class="ruby-ivar">@properties</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span> <span class="ruby-identifier">property_name</span>, <span class="ruby-identifier">property</span> <span class="ruby-operator">|</span>
    <span class="ruby-identifier">property</span>.<span class="ruby-identifier">rewrite_path</span>( <span class="ruby-identifier">depth</span>, <span class="ruby-identifier">name</span> )
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
          </div>
</div>

    </div>
  </body>
</html>
